import json
import os
import requests
import  execjs
import csv
cookies = {
    'btoken': '2NLNVTP31H6LGFKD6GSZKULB0HOTBCB2',
    'hy_data_2020_id': '1905e1417f2e95-06b4c580a190e4-4c657b58-1821369-1905e1417f31751',
    'hy_data_2020_js_sdk': '%7B%22distinct_id%22%3A%221905e1417f2e95-06b4c580a190e4-4c657b58-1821369-1905e1417f31751%22%2C%22site_id%22%3A211%2C%22user_company%22%3A105%2C%22props%22%3A%7B%7D%2C%22device_id%22%3A%221905e1417f2e95-06b4c580a190e4-4c657b58-1821369-1905e1417f31751%22%7D',
    'sajssdk_2020_cross_new_user': '1',
    'mp_8b1215dbcc852955566c4df66e547374_mixpanel': '%7B%22distinct_id%22%3A%20%221905e141a472b29-0489751b935eb6-4c657b58-1bcab9-1905e141a4829e4%22%2C%22%24device_id%22%3A%20%221905e141a472b29-0489751b935eb6-4c657b58-1bcab9-1905e141a4829e4%22%2C%22%24search_engine%22%3A%20%22bing%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fcn.bing.com%2F%22%2C%22%24initial_referring_domain%22%3A%20%22cn.bing.com%22%7D',
}

headers = {
    'accept': 'application/json',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'content-type': 'application/json',
    # 'cookie': 'btoken=2NLNVTP31H6LGFKD6GSZKULB0HOTBCB2; hy_data_2020_id=1905e1417f2e95-06b4c580a190e4-4c657b58-1821369-1905e1417f31751; hy_data_2020_js_sdk=%7B%22distinct_id%22%3A%221905e1417f2e95-06b4c580a190e4-4c657b58-1821369-1905e1417f31751%22%2C%22site_id%22%3A211%2C%22user_company%22%3A105%2C%22props%22%3A%7B%7D%2C%22device_id%22%3A%221905e1417f2e95-06b4c580a190e4-4c657b58-1821369-1905e1417f31751%22%7D; sajssdk_2020_cross_new_user=1; mp_8b1215dbcc852955566c4df66e547374_mixpanel=%7B%22distinct_id%22%3A%20%221905e141a472b29-0489751b935eb6-4c657b58-1bcab9-1905e141a4829e4%22%2C%22%24device_id%22%3A%20%221905e141a472b29-0489751b935eb6-4c657b58-1bcab9-1905e141a4829e4%22%2C%22%24search_engine%22%3A%20%22bing%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fcn.bing.com%2F%22%2C%22%24initial_referring_domain%22%3A%20%22cn.bing.com%22%7D',
    'origin': 'https://www.xiniudata.com',
    'priority': 'u=1, i',
    'referer': 'https://www.xiniudata.com/industry/newest?from=data',
    'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0',
}

#存储数据
def data_to_csv(data,fieldnames,filename):
    # 确定字段顺序 行标题
    # fieldnames = ['name', 'company', 'address', 'size', 'industry', 'welfare', 'salary', 'experience', 'education']
    #打开文件
    with open(filename,'a',encoding='utf-8',newline='') as csvfile:
        writer = csv.DictWriter(csvfile,fieldnames=fieldnames)
        #写入表头
        if os.stat(filename).st_size == 0: #判断表头是否已经生成 如果生成了 便不再写入表头
            writer.writeheader()
        #写入数据
        for i in data:
            writer.writerow(i)
    csvfile.close()
starts = [0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400,420,440,460,480,500,520,540,560,580,600,620,640,660,680,700,720,740,760,780,800,820,840,860,880,900,920]
params_list = []
for start in starts:
    #打开 js文件 对参数进行加密 获取加密数据
    jscode = open('./参数数据加密.js','r',encoding='utf-8').read()
    params1 = execjs.compile(jscode).call('main123' ,start)
    params_list.append(params1)
    # print(params)

filename = './犀牛数据.csv'
fieldnames = ['赛道', 'event', '最近获投', '最新更新title', 'brief','url']

for params in params_list:
    json_data = {
        'payload': params['payload'],  #加密参数
        'sig': params['sig'],  #加密参数
        'v': 1,
    }

    response = requests.post(
        'https://www.xiniudata.com/api2/service/x_service/person_industry_list/list_industries_by_sort',
        cookies=cookies,
        headers=headers,
        json=json_data,
    )
    # print(response.json())

    text1 =response.json()['d']
    # print(text1)

    #对数据进行解密操作
    jscode1 = open('./数据逆向.js','r',encoding='utf-8').read()
    text = execjs.compile(jscode1).call('main456',text1 )
    # print(text['list'][0])
    list_data= []
    for index in range(0,20):
        try:
        #这里要注意  json和字典的形式区别  json在大括号外还有引号  字典没有
            t = text['list'][index]['lastUpdateVO']['lastUpdateNews']
            json_data = json.loads(t)

            dic = {
                '赛道':text['list'][index]['name'],
                'event':text['list'][index]['event'],
                '最近获投': [d['name'] for d in text['list'][index]['companyVOs']],
                '最新更新title': json_data['title'],
                'url':json_data['link'],
                'brief': json_data['brief']
            }
            list_data.append(dic)
            print(dic['赛道']+'完成！')
        except:
            pass

    data_to_csv(list_data,fieldnames,filename)


